package com.whately.core.base.exception.handler;

import com.whately.core.base.model.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 主要用途：统一处理错误/异常(针对控制层)
 */
@Controller
@RequestMapping("${server.error.path:/error}")
public class GlobalErrorController implements ErrorController {

    /**
     * 错误信息的构建工具.
     */
    @Autowired
    private ErrorInfoBuilder errorInfoBuilder;

    /**
     * 错误信息页的路径
     */
    private final static String DEFAULT_ERROR_VIEW = "error";

    /**
     * 获取错误控制器的映射路径.
     */
    @Override
    public String getErrorPath() {
        String path =errorInfoBuilder.getErrorProperties().getPath();
        return path;
    }

    /**
     * 情况1：若预期返回类型为text/html,s则返回错误信息页(View).
     */
    /*@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request) {
        ErrorInfo errorInfo=errorInfoBuilder.getErrorInfo(request);
        ApiResponse  apiResponse = new ApiResponse(errorInfo.getApiResponseCode(),errorInfo.getApiResponseMessage(),errorInfo);
        return new ModelAndView(DEFAULT_ERROR_VIEW,"apiResponse", apiResponse);
    }*/

    /**
     * 情况2：其它预期类型 则返回详细的错误信息(JSON).
     */
    @RequestMapping
    @ResponseBody
    //@ExceptionHandler(value = {Exception.class})
    public ApiResponse error(HttpServletRequest request) {
        ErrorInfo errorInfo= errorInfoBuilder.getErrorInfo(request);
        return ApiResponse.builder().code(errorInfo.getApiResponseCode()).message(errorInfo.getApiResponseMessage()).data(errorInfo).build();
    }


}
